package com.inspur.demo.controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 生产者控制层
 * @author li.peng02
 */
@RestController
@RequestMapping("/producer")
public class ProducerController {

    private HttpServletRequest request;
    private HttpServletResponse response;

    @RequestMapping("/test/{name}")
    public String test(@PathVariable String name) {
        String contextPath = this.request.getRequestURL().toString();
        return "Hello, " + name + ", you are from \"" + contextPath + "\"!";
    }

    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
    }

    @RequestMapping("/getHeader")
    @ResponseBody
    public Map<String, Object> getHeader() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Map<String, Object> result = new HashMap<>();
        Enumeration<String> headerNames = this.request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String element = headerNames.nextElement();
            Enumeration<String> headers = this.request.getHeaders(element);
            List<String> values = new ArrayList<>();
            while (headers.hasMoreElements()) {
                values.add(headers.nextElement());
            }
            if (values.size() > 1) {
                result.put(element, values);
            } else {
                result.put(element, values.get(0));
            }
        }
        return result;
    }

    @RequestMapping("/getParam")
    @ResponseBody
    public Map<String, String[]> getParam() {
        response.addHeader("rep-id","123456");
        response.addHeader("rep-id","1234567");
        response.addHeader("rep-author","ponzio");
        response.addHeader("rep-password","p@ssw0rd");
        response.addHeader("rep-url","/42?user=ford&password=omg!what&flag=true");
        return request.getParameterMap();
    }

    @RequestMapping("/testRewriteLocationResponseHeader")
    @ResponseBody
    public Map<String, String> testRewriteLocationResponseHeader () {
        response.setHeader("Location","www.aaa.com/v2/testRewriteLocationResponseHeader");
        response.setHeader("PonzioLocation","www.aaa.com/v2/testRewriteLocationResponseHeader");
        Map<String,String> map = new HashMap<>(2);
        Collection<String> headerNames = response.getHeaderNames();
        for(String name:headerNames){
            String value = response.getHeader(name);
            map.put(name,value);
        }
        return map;
    }

    @GetMapping("/testRetry")
    public void testRetry(HttpServletRequest request) throws IOException {
        System.out.println(System.currentTimeMillis()+" retry....");
        throw new IOException();
    }
}
